package com.cgt.web.servlet;

import com.cgt.web.pojo.Teacher;
import com.cgt.web.pojo.query.Teacher1Query;
import com.cgt.web.service.ITea1Service;
import com.cgt.web.service.impl.Tea1ServiceImpl;
import com.cgt.web.util.PageResult;
import com.cgt.web.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/teacher1")
public class Teacher1Servlet extends BaseServlet{
	private ITea1Service tea1Service = new Tea1ServiceImpl();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		if(method == null || method.equals("")){
			method = "selectByPage";
		}
		switch (method){
			case "selectByPage":
				selectByPage(req,resp);
				break;
			case "deleteById":
				deleteById(req,resp);
				break;
			case "add":
				add(req,resp);
				break;
			case "selectById":
				selectById(req,resp);
				break;
			case "update":
				update(req,resp);
				break;
			case "deleteAll":
				deleteAll(req,resp);
				break;
		}
	}

	private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Teacher1Servlet.deleteAll");
		String[] ids = req.getParameterValues("ids[]");
		tea1Service.deleteAll(ids);
		toJSON (resp, Result.ok("删除成功"));
	}

	private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Teacher1Servlet.update");
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String address = req.getParameter("address");
		Teacher teacher = new Teacher(Integer.parseInt(id),name,Integer.parseInt(age),address);
		tea1Service.update(teacher);
		toJSON(resp,Result.ok("修改成功"));
	}

	private void selectById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Teacher1Servlet.selectById");
		String id = req.getParameter("id");
		Teacher teacher = tea1Service.selectById(Integer.parseInt(id));
		toJSON(resp,Result.ok(teacher));
	}

	private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Teacher1Servlet.add");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String address = req.getParameter("address");
		Teacher teacher = new Teacher(name,Integer.parseInt(age),address);
		tea1Service.add(teacher);
		toJSON(resp,Result.ok("添加成功"));
	}

	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Teacher1Servlet.deleteById");
		String id = req.getParameter("id");
		tea1Service.deleteById(Integer.parseInt(id));
		Result result = new Result(0,"删除成功");
		toJSON(resp,result);
	}

	private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		System.out.println("Teacher1Servlet.selectByPage");
		String page = req.getParameter("page");
		String limit = req.getParameter("limit");
		String name = req.getParameter("name");
		String age = req.getParameter("age");
		String address = req.getParameter("address");
		Teacher1Query teacher1Query = new Teacher1Query(Integer.parseInt(page),Integer.parseInt(limit),name,age,address);
		PageResult<Teacher> pageResult = tea1Service.selectByPage(teacher1Query);
		toJSON(resp,pageResult);
	}
}
